package com.ysstech.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.service.DicvalueService;
import com.ysstech.finance.entity.SubjectAdjustAuxilary;
import com.ysstech.finance.mapper.SubjectAdjustAuxilaryMapper;
import com.ysstech.finance.service.SubjectAdjustAuxilaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 科目核算项和辅助项对应关系表 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2022-06-27
 */
@Service
public class SubjectAdjustAuxilaryServiceImpl extends ServiceImpl<SubjectAdjustAuxilaryMapper, SubjectAdjustAuxilary> implements SubjectAdjustAuxilaryService {

    @Autowired
    private SubjectAdjustAuxilaryMapper subjectAdjustAuxilaryMapper;
    @Autowired
    private DicvalueService dicvalueService;

    @Override
    public List<SubjectAdjustAuxilary> queryAdjustAuxilaryBySubjectId(SubjectAdjustAuxilary subjectAdjustAuxilary) throws Exception {
        if (null == subjectAdjustAuxilary || StringUtils.isBlank(subjectAdjustAuxilary.getAdjustCode())
                || StringUtils.isBlank(subjectAdjustAuxilary.getProjectId())) {
            throw new BusinessException("获取科目辅助项信息失败，请稍后重试！");
        }
        // 获取科目辅助项信息
        List<SubjectAdjustAuxilary> list = subjectAdjustAuxilaryMapper.queryAdjustAuxilaryBySubjectId(subjectAdjustAuxilary);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        for (SubjectAdjustAuxilary adjustAuxilary : list) {
            if (null == adjustAuxilary || StringUtils.isBlank(adjustAuxilary.getAdjustCode())) {
                continue;
            }
            List<Dicvalue> dicvalueList = dicvalueService.getCacheDicValueList(adjustAuxilary.getAuxilaryCode());
            adjustAuxilary.setDicAuxilaryCodeList(dicvalueList);
            if (!CollectionUtils.isEmpty(dicvalueList) && null != dicvalueList.get(0)) {
                adjustAuxilary.setAuxilaryName(dicvalueList.get(0).getName());
            }
        }
        return list;
    }
}
